class MapManager {




    private maps: { [index: string]: GameMap } = {};
    private container: renderer.DisplayObjectContainer;
    private currentMap = "map2"
    parseFromConfig(id: string, obj: any) {
        this.maps[id] = new GameMap(obj.map[0]);
    }

    initContainer(container: renderer.DisplayObjectContainer) {
        this.container = container;
    }

    changeMap(mapId: string) {
        this.currentMap = mapId;
        const map = this.getMap();
        this.container.removeAllChildren();
        this.container.addChild(map);

        const mapStartNode = { x: 0, y: 0 };
        map.grid.setStartNode(mapStartNode.x, mapStartNode.y);
        map.addToRoleContainer(user.view)
        user.view.x = mapStartNode.x * TILE_SIZE;
        user.view.y = mapStartNode.y * TILE_SIZE;

        map.addEventListener("click", (eventData: any) => {

            const x = eventData.localX;
            const y = eventData.localY;

            const row = Math.floor(x / TILE_SIZE);
            const col = Math.floor(y / TILE_SIZE);

            console.log("目标：" + row + "," + col);


            const info = map.getTileNodeInfo(row, col);


            pool.clear();


            const walk = new WalkCommand(row, col);
            const walkToNpc = new WalkCommand(row, col + 1);
            const walkToEnemy = new WalkCommand(row + 1, col);



            //pool.add(walk);


            const npc = map.getNpcInfo(row, col);
            const item = map.getItemInfo(row, col);
            const enemy = map.getEnemyInfo(row, col);

            if (npc) {
                console.log("NPC"!!!);

                const talk = new TalkCommand(npc);
                pool.add(walkToNpc);
                console.log(walkToNpc);

                pool.add(talk);
            }
            else if (item) {
                pool.add(walk);

                const pick = new PickCommand(item);
                pool.add(pick)
            }
            else if (enemy) {
                pool.add(walkToEnemy);
                if (user.level >= 12) {

                    const intervalCount = setInterval(() => {
                        alert("您通过了游戏！");
                        window.location.reload();
                    }, 2000);

                }
            }
            else {
                pool.add(walk);
            }


            // if (npc) {
            //     const talk = new TalkCommand(npc);
            //     pool.add(walkToNpc);
            //     pool.add(talk)
            //     const gotoNextMap = new GotoNextMapCommand("map1");
            //     pool.add(gotoNextMap)
            // }


            // if (item) {
            //     const pick = new PickCommand(item);
            //     pool.add(pick)
            // }

            // if (enemy) {
            //     pool.add(walkToEnemy);
            //     if (user.level >= 12) {

            //         const intervalCount = setInterval(() => {
            //             alert("您通过了游戏！");
            //             window.location.reload();
            //         }, 2000);

            //     }
            // }


            // {
            //     const walk = new WalkCommand(row, col);
            //     const walkToNpc = new WalkCommand(row, col + 1);
            //     const walkToEnemy = new WalkCommand(row + 1, col);


            //     const npc = map.getNpcInfo(row, col);
            //     const item = map.getItemInfo(row, col);
            //     const enemy = map.getEnemyInfo(row, col);
            //     // 

            //     if (npc) {
            //         const talk = new TalkCommand(npc);
            //         pool.add(walkToNpc);
            //         pool.add(talk)
            //     }
            //     else if (item) {
            //         pool.add(walk);

            //         const pick = new PickCommand(item);
            //         pool.add(pick)
            //     }
            //     else if (enemy) {
            //         pool.add(walkToEnemy);
            //         if (user.level >= 12) {

            //             const intervalCount = setInterval(() => {
            //                 alert("您通过了游戏！");
            //                 window.location.reload();
            //             }, 2000);

            //         }
            //     }
            //     else {
            //         pool.add(walk);
            //     }
            // }






            pool.execute();


        })
    }

    getMap() {
        return this.maps[this.currentMap]
    }
}



//const RIVER = 0;
//const GRESS = 1;
const TILE_SIZE = 64;




class GameMap extends renderer.DisplayObjectContainer {

    grid: astar.Grid
    private config: any;
    private itemConfig: { [index: string]: Item } = {};
    private npcConfig: { [index: string]: Npc } = {};
    private enemyConfig: { [index: string]: Enemy } = {};

    private tileContainer = new renderer.DisplayObjectContainer();
    private itemContainer = new renderer.DisplayObjectContainer();
    private roleContainer = new renderer.DisplayObjectContainer();

    constructor(config: any) {
        super();
        this.config = config;
        this.grid = new astar.Grid(8, 8);
        this.addChild(this.tileContainer);
        this.addChild(this.itemContainer);
        this.addChild(this.roleContainer);



        for (let item of this.config.mapSetting) {

            const texture = new Image();
            switch (item.tile) {
                case "TREE":
                    texture.src = this.config.treeTexture;
                    break;
                case "GRASS":
                    texture.src = this.config.grassTexture;
                    break;
                default:
                    alert("错误的地图配置!!" + JSON.stringify(item))
                    break;
            }
            const tile = new renderer.Bitmap(TILE_SIZE * item.x, TILE_SIZE * item.y, texture);
            let walkable = item.tile == "TREE" ? false : true;


            if (item.npc || item.enemy) {
                walkable = false;
            }
            this.grid.setWalkable(item.x, item.y, walkable);

            this.tileContainer.addChild(tile)

            if (item.item) {

                const swordTexture = new Image();
                swordTexture.src = 'knife.png';

                const weaponView = new renderer.Bitmap(0, 0, swordTexture);
                weaponView.x = TILE_SIZE * item.x;
                weaponView.y = TILE_SIZE * item.y;
                const weaponItem = new Item();
                weaponItem.name = "刀";
                weaponItem.attack = 25;
                weaponItem.x = item.x;
                weaponItem.y = item.y;
                const key = item.x + "_" + item.y;
                this.itemConfig[key] = weaponItem;
                weaponItem.view = weaponView;
                this.itemContainer.addChild(weaponView)
            }
            if (item.npc) {
                const npc = npcManager.getNpcById(item.npc);
                this.roleContainer.addChild(npc.view);
                npc.x = item.x;
                npc.y = item.y;
                npc.view.x = npc.x * TILE_SIZE;
                npc.view.y = npc.y * TILE_SIZE;
                const key = npc.x + "_" + npc.y;
                //id一样只能有一个NPC??
                this.npcConfig[key] = npc;
            }
            if (item.enemy) {

                //  const enemy = new Enemy(item.enemy);
                const enemy = new Enemy();
                enemy.createEnemy(enemy);
                enemy.avatar.image = enemyAvatar;
                this.roleContainer.addChild(enemy.avatar);
                enemy.x = item.x;
                enemy.y = item.y;
                enemy.avatar.x = enemy.x * TILE_SIZE;
                enemy.avatar.y = enemy.y * TILE_SIZE;
                const key = enemy.x + "_" + enemy.y;
                this.enemyConfig[key] = enemy;
                // console.log("enemyConfig" + this.enemyConfig[key]);

            }
            // this.grid.setWalkable(item.x, item.y, walkable);
        }



    }

    addToRoleContainer(child: renderer.DisplayObject) {
        if (child.parent) {
            child.parent.removeChild(child);
        }
        this.roleContainer.addChild(child);
    }

    getItemInfo(x: number, y: number) {
        return this.itemConfig[x + "_" + y];
    }

    getNpcInfo(x: number, y: number) {
        const key = x + "_" + y;
        return this.npcConfig[key]
    }
    getEnemyInfo(x: number, y: number) {
        const key = x + "_" + y;
        return this.enemyConfig[key]
    }

    removeItem(item: Item) {
        const key = item.x + "_" + item.y;
        delete this.itemConfig[key];
        this.removeChild(item.view);
    }

    getTileNodeInfo(x: number, y: number) {
        for (let item of this.config) {
            if (item.x == x && item.y == y) {
                return item;
            }
        }
        return null;
    }
}



const mapManager = new MapManager();